<?php
/**
 * Silverstripe cms tree  Extension
 * Powered by jQuery
 * @author rgodinho
 */
class SSCustomTreeListExtension extends Extension {
	/**
	 * To load jQuery Library. 
	 * If loaded from the main project should't be loaded by the extension
	 * Put SSCustomTreeListExtension::$load_jQuery = false at _config.php
	 * @var boolean
	 */
	public static $load_jQuery = true;
	
	/**
	 * Define the theme for the tree
	 * @var string
	 */
	public static $theme_tree = 'default';
	
	function extraStatics() {
    return array(
    );
  }

  public function Subject() {
    return $this->owner;
  }

  /**
   * Render a template
   * @param $template template_file
   * @return html render
   */
  public function Template($template) {
    return $this->owner->renderWith($template);
  }

   /**
   * Render Tree
   * @return html rendered
   */
  public function getSSCustomTreeList() {
		if (SSCustomTreeListExtension::$load_jQuery) {
  			Requirements::javascript(SSCUSTOMTREELIST . '/javascript/jquery-1.4.min.js');
		}
		Requirements::javascript(SSCUSTOMTREELIST . '/javascript/jsTree/lib/jquery.cookie.js');
		Requirements::javascript(SSCUSTOMTREELIST . '/javascript/jsTree/jquery.tree.js');
		Requirements::javascript(SSCUSTOMTREELIST . '/javascript/jsTree/plugins/jquery.tree.cookie.js');
		Requirements::javascript(SSCUSTOMTREELIST . '/javascript/sscustomtreelist.js');				
		
Requirements::customScript(<<<JS
jQuery(document).ready(function() {		
jQuery(function () { jQuery("#sscustomtree").tree({	plugins : {	cookie : { prefix : "jstree_" }}});});});		
JS
);    		
		// -- Extension tree style file. 
  	 	Requirements::css(SSCUSTOMTREELIST . '/javascript/jsTree/themes/' . SSCustomTreeListExtension::$theme_tree . '/style.css');
  	   
		return $this->owner->renderWith('TreeCss');
  }  
}